'use strict';

const Controller = require('egg').Controller;


function toInt(str) {
  if (typeof str === 'number') return str;
  if (!str) return str;
  return parseInt(str, 10) || 0;
}


class ActivitesController extends Controller {


  constructor(ctx) {
    super(ctx);

    this.ActivityInfo = {
      id: { type: 'string', required: true, allowEmpty: false },
    };

    this.DestroyActivityInfo = {
      id: { type: 'string', required: true, allowEmpty: false },
    };

    this.QueryActivity = {
      page: { type: 'int', required: false, allowEmpty: true },
      size: { type: 'int', required: false, allowEmpty: true },
    };


    this.CreateActivity = {
      title: { type: 'string', required: true, allowEmpty: true },
      content: { type: 'string', required: true, allowEmpty: true },
      images: { type: 'string', required: false, allowEmpty: true },
      plantforms: { type: 'int', required: false, allowEmpty: true },
      cooperations: { type: 'int', required: false, allowEmpty: true },
      start: { type: 'datatime', required: true, allowEmpty: false },
      end: { type: 'datatime', required: true, allowEmpty: false },
      des: { type: 'string', required: false, allowEmpty: true },
    };


  }

  /**
   *
   * @api {GET} /api/activity/list 获取列表
   * @apiName 获取列表
   * @apiGroup Activity
   *
   *
   * @apiParam  {String} paramName description
   *
   * @apiSuccess (200) {type} name description
   *
   * @apiParamExample  {type} Request-Example:
   * {
   *     property : value
   * }
   *
   *
   * @apiSuccessExample {type} Success-Response:
   * {
   *     property : value
   * }
   *
   *
   */
  async index() {
    const ctx = this.ctx;
    const res = await ctx.service.activites.index();
    ctx.response.success({ ctx, res });
  }

  async info() {
    const ctx = this.ctx;
    ctx.validate(this.DestroyActivityInfo, ctx.params);
    const id = ctx.params.id;
    console.log(id);
    const res = await ctx.service.activites.info(toInt(id));
    ctx.response.success({ ctx, res });
  }
  /**
   *
   * @api {GET} api/activity/:page/:size 获取活动
   * @apiGroup Activity
   *
   *
   * @apiParam  {String} paramName description
   *
   * @apiSuccess (200) {type} name description
   *
   * @apiParamExample  {type} Request-Example:
   * {
   *     property : value
   * }
   *
   *
   * @apiSuccessExample {type} Success-Response:
   * {
   *     property : value
   * }
   *
   *
   */
  async query() {
    const ctx = this.ctx;
    ctx.validate(this.QueryActivity);
    const { page, size } = ctx.params;
    const res = await ctx.service.activites.query(page, size);
    ctx.response.success({ ctx, res });
  }

  /**
   *
   * @api {POST} /api/activity 创建活动
   * @apiGroup Activity
   *
   *
   * @apiParam  {String} paramName description
   *
   * @apiSuccess (200) {type} name description
   *
   * @apiParamExample  {type} Request-Example:
   * {
   *     property : value
   * }
   *
   *
   * @apiSuccessExample {type} Success-Response:
   * {
   *     property : value
   * }
   *
   *
   */
  async create() {
    const ctx = this.ctx;
    ctx.validate(this.CreateActivity);
    const res = await ctx.service.activities.create(ctx.body);
    ctx.response.success({ ctx, res });
  }

  /**
   *
   * @api {POST} /api/activity/edit 修改活动
   * @apiGroup Activity
   *
   *
   * @apiParam  {String} paramName description
   *
   * @apiSuccess (200) {type} name description
   *
   * @apiParamExample  {type} Request-Example:
   * {
   *     property : value
   * }
   *
   *
   * @apiSuccessExample {type} Success-Response:
   * {
   *     property : value
   * }
   *
   *
   */
  async eidt() {

  }

  /**
   *
   * @api {POST} /api/activity/del 删除活动
   * @apiGroup Activity
   *
   *
   * @apiParam  {String} paramName description
   *
   * @apiSuccess (200) {type} name description
   *
   * @apiParamExample  {type} Request-Example:
   * {
   *     property : value
   * }
   *
   *
   * @apiSuccessExample {type} Success-Response:
   * {
   *     property : value
   * }
   *
   *
   */
  async destroy() {

  }


}

module.exports = ActivitesController;
